package com.zige.zige.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.zige.zige.pojo.Place;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by song on 2015/4/23.
 */
public class ProviceCityDao {


    SQLiteOpenHelper helper;

    public ProviceCityDao(Context context) {
        helper = LittlezebraDB.getInstance(context);
    }


    public List<Place> qureyAllProvice() {
        List<Place> list = null;
        SQLiteDatabase db = helper.getReadableDatabase();
        if (db.isOpen()) {
            Cursor c = db.query("Province", new String[]{"_id", "Province"}, null, null, null, null, null);
            list = new ArrayList<>();
            while (c.moveToNext()) {
                String id = c.getString(0);
                String name = c.getString(1);
                String superId = "0";
                list.add(new Place(id, name, superId));

            }
            c.close();
        }
        db.close();
        return list;
    }

    public List<Place> qureyAllCityByProvice(String proviceId) {
        List<Place> list = null;
        SQLiteDatabase db = helper.getReadableDatabase();
        if (db.isOpen()) {
            Cursor c = db.query("City", new String[]{"_id", "City", "Province_id"}, "Province_id=?", new String[]{proviceId}, null, null, null);
            list = new ArrayList<>();
            while (c.moveToNext()) {
                String id = c.getString(0);
                String name = c.getString(1);
                String superId = c.getString(2);
                list.add(new Place(id, name, superId));
            }
            c.close();
        }
        db.close();
        return list;
    }

}